<template>
    <div class="flex">
        <div class="chart">
            <V3Echarts :height="props.height" :options="option" container="container3" isFirst />
        </div>

        <div class="legend">
            <div class="box" v-for="(item, index) in props.content" :key="item.name">
                <div class="item" v-if="item.name">
                    <div class="count">
                        {{ item.value }}
                        <span class="unit">{{ item.unit }}</span>
                    </div>
                    <div class="item-left">
                        <div class="label">{{ item.name }}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import * as echarts from 'echarts'
import V3Echarts from '@/stories/components/V3Echarts/index.vue'

interface contentType {
    name: string
    unit: string
    value: number
}

const props = defineProps({
    chartData: {
        type: Array,
        default: () => {
            return [
                {
                    name: '占用率',
                    value: 5
                },
                {
                    name: '未占用率',
                    value: 46
                }
            ]
        }
    },
    content: {
        type: Array<contentType>,
        default: () => {
            return [
                {
                    name: '未占用',
                    unit: '间',
                    value: 46
                },
                {
                    name: '已占用',
                    unit: '间',
                    value: 5
                },
                {
                    name: '已预约',
                    unit: '间',
                    value: 6
                }
            ]
        }
    },
    isShowName: { type: Boolean, default: true },
    isShowPercent: {
        type: Boolean,
        default: false
    },
    color: {
        type: Array,
        default: () => {
            return ['#a183e2', '#fe8151', '#00fffc', '#64baff', '#b4b3ca', '#f14313', '#d6a33d']
        }
    },
    height: {
        type: Number,
        default: 250
    }
})
let option = ref({})
let sum = 0

const initChart = () => {
    const colorCopy = JSON.parse(JSON.stringify(props.color))

    const chartDataCopy = JSON.parse(JSON.stringify(props.chartData))
    sum = chartDataCopy.reduce((prev: any, cur: any) => {
        return prev + cur.value
    }, 0)
    chartDataCopy.forEach((item: any, index: number) => {
        item.itemStyle = {
            //颜色渐变
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: index % 2 == 0 ? colorCopy[index] : 'transparent' },
                { offset: 0.6, color: 'rgba(149,149,149,0.5)' },
                { offset: 1, color: index % 2 == 0 ? 'transparent' : colorCopy[index] }
            ])
        }
    })

    option.value = {
        color: colorCopy,
        series: [
            {
                type: 'pie',
                radius: ['55%', '80%'],
                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                emphasis: {
                    scale: { hoverAnimation: false },
                    label: {
                        show: true,
                        color: '#fff',
                        fontSize: 14,
                        fontWeight: 'bold',
                        formatter: function (data: any) {
                            if (props.isShowName) {
                                return '{value|' + ((data.value / sum) * 100).toFixed(2) + '}%' + '\n' + '{name|' + data.name + '}'
                            } else {
                                return '{value|' + ((data.value / sum) * 100).toFixed(2) + '}%'
                            }
                        },
                        rich: {
                            value: {
                                fontFamily: 'SJyunhei',
                                fontSize: 14,
                                fontWeight: '400'
                            },
                            name: {
                                fontFamily: 'SJyunhei',
                                fontSize: 10,
                                fontWeight: '400'
                            }
                        }
                    }
                },
                data: chartDataCopy
            }
        ]
    }
}

watch(
    () => props,
    newValue => {
        initChart()
    },
    { immediate: true, deep: true }
)
</script>

<style lang="scss" scoped>
.flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: v-bind('props.height');
    color: #ffffff;
    .chart {
        width: 35%;
        position: relative;
        margin-right: 15px;
        .chart-back {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
    }

    .legend {
        width: 65%;
        height: 80%;
        display: flex;
        justify-content: space-around;
        padding-top: 10px;
        padding-bottom: 10px;
        .box {
            width: 100%;
            .item {
                height: 100%;
                width: 50%;
                padding: 5px;
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                background: url('../img/Concise_boxbg.png') no-repeat;
                background-size: 100% 100%;
                margin-bottom: 10px;
                .item-left {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    text-align: center;
                    .label {
                        font-size: 24px;
                        color: #ddd;
                        font-family: OPPOSans;
                    }
                }

                .count {
                    font-size: 26px;
                    margin-bottom: 10px;
                    font-family: SJyunhei;
                    text-align: right;
                    .unit {
                        font-size: 24px;
                        font-family: OPPOSans;
                        opacity: 0.5;
                    }
                }
            }

            &:nth-child(2) {
                .item {
                    .item-left {
                        .label {
                            color: #826d4c;
                        }
                    }
                }
            }
            &:nth-child(3) {
                .item-left {
                    .label {
                        color: #73a1c5;
                    }
                }
            }
        }
    }
}
</style>
